package ThreadPool;

import java.util.concurrent.locks.ReentrantLock;

/**
 * ReentrantLock锁
 */
public class demo7 {
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        // 默认是非公平锁
        ReentrantLock lock = new ReentrantLock();

        // 公平锁
        ReentrantLock lock2 = new ReentrantLock(true);

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                try {
                    lock.lock();
                    count++;
                } finally {
                    lock.unlock();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                try {
                    lock.lock();
                    count++;
                } finally {
                    lock.unlock();
                }
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println(count);
    }
}
